package com.swipemaster.record;

import java.io.Serializable;
import java.util.HashMap;

import android.annotation.SuppressLint;

import com.swipemaster.common.Const;
import com.swipemaster.levels.levelinfo.LevelsInfo;

/** All the information that user can change and that needs to be saved. His name, high scores etc. */
public class Records implements Serializable
{
	public final LevelsInfo levelsInfo = new LevelsInfo();
	public String username;

	/** SparseArray is not Serializable. There will be less then 100 elements in map, so no difference in speed. */
	@SuppressLint("UseSparseArrays")
	private final HashMap<Integer, Record> records = new HashMap<Integer, Record>();
	private static final long serialVersionUID = 1L;

	public Records()
	{
		for (int i = 0; i < Const.LEVELS; i++)
		{
			if (levelsInfo.getInfo(i).isPuzzle)
			{
				records.put(i, new PuzzleRecord(i));
			}
			else
			{
				records.put(i, new SurvivalRecord(i));
			}
		}
		records.put(RecordId.CHALLENGE, new ChallengeRecord());
		records.put(RecordId.CUSTOM_GAME, new IntegerRecord(RecordId.CUSTOM_GAME));
		records.put(RecordId.GAMES_PLAYED, new IntegerRecord(RecordId.GAMES_PLAYED));
		records.put(RecordId.STARS, new IntegerRecord(RecordId.STARS));
		records.put(RecordId.CUSTOM_GAMES_PLAYED, new IntegerRecord(RecordId.CUSTOM_GAMES_PLAYED));
		records.put(RecordId.CHALLENGE_GAMES_PLAYED, new IntegerRecord(RecordId.CHALLENGE_GAMES_PLAYED));
		records.put(RecordId.LEVEL_GAMES_PLAYED, new IntegerRecord(RecordId.LEVEL_GAMES_PLAYED));
	}

	public Record getRecord(int id)
	{
		final Record record = records.get(id);
		if (record == null)
		{
			throw new RuntimeException("No record for id = " + id);
		}
		return record;
	}

	public Record getRecord(RecordId recordId)
	{
		return getRecord(recordId.getId());
	}

	public boolean isNewRecordAndUpdate(Record newRecord)
	{
		final Record oldRecord = getRecord(newRecord.recordId);
		if (newRecord.isBetter(oldRecord))
		{
			records.put(oldRecord.recordId.getId(), newRecord);
			return true;
		}
		return false;
	}

}
